package com.kj.xuyuan.http;

import android.content.Context;
import android.util.Log;
import com.kj.xuyuan.base.Constants;
import com.kj.xuyuan.util.LogUtils;
import com.kj.xuyuan.util.NetworkUtil;
import com.kj.xuyuan.util.SPUtils;
import com.kj.xuyuan.util.ToastUtil;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.HttpHeaders;
import java.io.File;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Created by guchunyan on 2018/12/15.
 * 请求简单封装了一下，方便使用
 */

public class OkGoUtil {

    public static <T> void getRequets(Context context, String url, JsonCallback<T> callback) {
        LogUtils.e("OkGoUtil", "method get"+url);
        if(NetworkUtil.isNetworkConnected(context)){
            OkGo.<T>get(url)
                    .tag(context)
                    .execute(callback);
        }else{
            ToastUtil.showToast(context,"网络连接错误!");
        }

    }

    public static <T> void getRequets(Context context, String url, Map<String, String> params, JsonCallback<T> callback) {
        if(NetworkUtil.isNetworkConnected(context)){
            StringBuilder sb = new StringBuilder();
            sb.append(UrlConstants.Base_Http).append(url);
            if (params != null && params.size() > 0) {
                sb.append("?");
                Set<String> keys = params.keySet();
                for (String key : keys) {
                    String value = params.get(key);
                    sb.append(key).append("=").append(value).append("&");
                }
                sb.append("token=").append(SPUtils.getStringData(context,Constants.TOKEN,"")).append("&");
                url = sb.deleteCharAt(sb.length() - 1).toString();
            }else{
                sb.append("?").append("token=").append(SPUtils.getStringData(context,Constants.TOKEN,""));
                url = sb.toString();
            }

            LogUtils.e("OkGoUtil", "method get"+url);
            OkGo.<T>get(url)
                    .tag(context)
                    .execute(callback);
        }else{
            ToastUtil.showToast(context,"网络连接错误!");
        }
    }

    private static String format(String url) {
        return UrlConstants.Base_Http + url;
    }

    private static HttpHeaders getHeaders(Context context) {
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.put("X-Access-Token", "");
        httpHeaders.put("token", SPUtils.getStringData(context, Constants.TOKEN,""));
        return httpHeaders;
    }

    public static <T> void postRequest(Context context, String url, Map json, JsonCallback<T> callback) {
        Log.e("OkGoUtil", "method post");
        if(NetworkUtil.isNetworkConnected(context)){
            OkGo.<T>post(format(url))
                    .tag(context)
                    .headers(getHeaders(context))
                    .params(json)
                    .params("token",SPUtils.getStringData(context,Constants.TOKEN,""))
                    .execute(callback);
        }else{
            ToastUtil.showToast(context,"网络连接错误!");
        }
    }

    public static <T> void postJsonRequest(Context context, String url, String json, JsonCallback<T> callback) {
        Log.e("OkGoUtil", "method post");
        if(NetworkUtil.isNetworkConnected(context)){
            OkGo.<T>post(format(url))
                    .tag(context)
                    .headers(getHeaders(context))
                    .upJson(json)
                    .execute(callback);
        }else{
            ToastUtil.showToast(context,"网络连接错误!");
        }
    }

    public static <T> void postSingleFile(Context context, String url, File file, JsonNoEncryptionCallback<T> callback) {
        Log.e("OkGoUtil", "method post");
        if(NetworkUtil.isNetworkConnected(context)){
            OkGo.<T>post(format(url))
                    .tag(context)
                    .params("file",file)
                    .params("token",SPUtils.getStringData(context,Constants.TOKEN,""))
                    .isMultipart(true)
                    .execute(callback);
        }else{
            ToastUtil.showToast(context,"网络连接错误!");
        }
    }

    public static <T> void postFiles(Context context, String url, List<File> fileList, JsonNoEncryptionCallback<T> callback) {
        if(context == null)
            return;

        if(NetworkUtil.isNetworkConnected(context)){
            OkGo.<T>post(format(url))
                    .tag(context)
                    .addFileParams("file",fileList)
                    .params("token",SPUtils.getStringData(context,Constants.TOKEN,""))
                    .isMultipart(true)
                    .execute(callback);
        }else{
            ToastUtil.showToast(context,"网络连接错误!");
        }
    }
}
